package com.pek.control.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.pek.annontation.Permit.Permit;
import com.pek.annontation.Permit.UserPermit;
import com.pek.control.entity.ArticleEntity;
import com.pek.control.entity.ArticleEntity;
import com.pek.control.service.IArticleService;
import com.pek.util.BaseController;
import com.pek.util.RestResult;
import org.springframework.web.bind.annotation.*;

/**
 * 文章
 *
 * @author 夜雨
 * @domain www.yeyv.net
 */
@RestController
@RequestMapping("/article")
public class ArticleController extends BaseController<ArticleEntity> {
    private IArticleService articleService;

    public ArticleController(IArticleService articleService) {
        super(articleService);
        this.articleService = articleService;
    }

    /**
     * 查询列表
     */
    @PostMapping("/list")
    public RestResult<Object> list(@RequestParam(defaultValue = "0") Integer size,
                                   @RequestBody(required = false) ArticleEntity article,
                                   @RequestParam(defaultValue = "0") Integer pageSize,
                                   @RequestParam(defaultValue = "0") Integer pageNo) {
        LambdaQueryWrapper<ArticleEntity> entityWrapper = new LambdaQueryWrapper<>();
        if (article != null) {
            entityWrapper.like(article.getType() != null, ArticleEntity::getType, article.getType());
            entityWrapper.like(StringUtils.isNotBlank(article.getTitle()), ArticleEntity::getTitle, article.getTitle());
            entityWrapper.like(StringUtils.isNotBlank(article.getContent()), ArticleEntity::getContent, article.getContent());
            entityWrapper.like(StringUtils.isNotBlank(article.getAuthor()), ArticleEntity::getAuthor, article.getAuthor());
        }
        return super.list(size, pageSize, pageNo, entityWrapper);
    }

    /**
     * 根据id获取
     */
    @GetMapping("/query")
    public RestResult<ArticleEntity> query(@RequestParam String id) {
        return super.query(id);
    }

    @PostMapping("/save")
    @Permit(value = {UserPermit.INSERT}, time = 500)
    public RestResult<ArticleEntity> save(@RequestBody ArticleEntity article) {
        return super.save(article);
    }

    @PostMapping("/edit")
    @Permit(value = {UserPermit.UPDATE}, time = 2000)
    public RestResult<ArticleEntity> edit(@RequestBody ArticleEntity article) {
        return super.edit(article);
    }

    @PostMapping("/del")
    @Permit(value = {UserPermit.DELETE}, time = 2000)
    public RestResult<ArticleEntity> del(@RequestParam String id) {
        return super.delete(id);
    }
}
